package cn.jinjimi.app.main.widget.biz;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.app.R;

/**
 * 形象照处理中或处理完
 * <p/>
 * Created by Jinsen on 2015/6/19.
 */
public class StatePhotoIngDoneView extends RelativeLayout implements PersonalPhotoStateView {

    /**
     * 图的地址
     */
    public String mUrl = null;

    /**
     * 图片的宽高比
     */
    public float mPhotoScale = 1.0f;

    TextView mTitleTv = null;
    ImageView mPhotoPicIv = null;

    TextView mIngView = null;//处理中
    TextView mIngDescTitle = null;

    //审核成功
    View mDoneView = null;
    ImageView mDoneMarkIv = null;

    Context mContext = null;

    //图片处理状态
    int mState = 0;//-1未上传，0审核中，1审核成功，2审核失败,3处理完成

    public StatePhotoIngDoneView(Context context) {
        this(context, null, 0);
    }

    public StatePhotoIngDoneView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StatePhotoIngDoneView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        inflate(context, R.layout.personal_photo_state_doing_layout, this);

        mContext = context;

        mTitleTv = (TextView) findViewById(R.id.state_doing_state_title_textView);
        mPhotoPicIv = (ImageView) findViewById(R.id.state_doing_photo_imageView);

        mIngView = (TextView) findViewById(R.id.state_doing_desc_title_textView);
        mIngDescTitle = (TextView) findViewById(R.id.state_doing_desc_detail_textView);

        mDoneMarkIv = (ImageView) findViewById(R.id.state_doing_done_imageView);
        mDoneView = findViewById(R.id.state_doing_done_layout);

        resetPhotoViewSize();
        setUIByState();
    }

    @Override
    public void setPhotoUrl(String url) {
        mUrl = url;

        mPhotoPicIv.setImageURI(Uri.parse(mUrl));
    }

    @Override
    public void setPhotoScale(float scale) {
        mPhotoScale = scale;

        resetPhotoViewSize();
    }

    @Override
    public void resetPhotoViewSize() {
        int width = AndroidUtils.getWindowWidth(mContext);
        int dp60 = mContext.getResources().getDimensionPixelSize(R.dimen.dp_60);

        int picWidth = (width - dp60 - dp60);
        mPhotoPicIv.getLayoutParams().width = picWidth;
        mPhotoPicIv.getLayoutParams().height = (int) (mPhotoScale * picWidth);
    }

    @Override
    public void setState(int state) {
        mState = state;
        setUIByState();
    }

    @Override
    public void setPhotoBitmap(Bitmap photo) {
        if (photo != null)
            mPhotoPicIv.setImageBitmap(photo);
    }

    private void setUIByState() {
        if (mState == 3) {
            mTitleTv.setText("处理后效果");
            mTitleTv.setBackgroundResource(R.drawable.personalphoto_state_normal_worked_bg);

            mIngDescTitle.setVisibility(GONE);
            mIngView.setVisibility(View.GONE);
            mDoneMarkIv.setVisibility(View.VISIBLE);
            mDoneView.setVisibility(View.VISIBLE);
        } else {
            mTitleTv.setText("我的原始照片");
            mTitleTv.setBackgroundResource(R.drawable.personalphoto_state_normal_primeval_bg);

            mIngDescTitle.setVisibility(VISIBLE);
            mIngView.setVisibility(View.VISIBLE);
            mDoneMarkIv.setVisibility(View.GONE);
            mDoneView.setVisibility(View.GONE);
        }
    }
}
